<!--
 * @FileDescription 视窗页
 * @Author lz
 * @Date 20230606 16:56:58
 * @LastEditors lz
 * @LastEditTime 20230606 16:56:58
-->
<template>
  <div class="wp">
    <hdr></hdr>
    <a-tooltip placement="bottom" v-if="HomeContent.Default.BDT.value == '0'">
      <template #title>
        <span>返回</span>
      </template>
      <rollback-outlined :style="{ color: '#FFF' }" class="ico_roll" @click="roll" />
    </a-tooltip>
    <a-tooltip placement="bottom">
      <template #title>
        <span>电子白板</span>
      </template>
      <edit-outlined class="edit" @click="whiteboard()" />
    </a-tooltip>
    <a-tooltip placement="bottom" v-if="HomeContent.Default.SubBut.value">
      <template #title>
        <span>提交</span>
      </template>
      <UploadOutlined
        :class="HomeContent.Default.BDT.value == '0' ? 'submitTo' : 'submitToRoll'"
        @click="sub"
      />
      <!-- <UploadOutlined class="submitToRoll" @click="sub" /> -->
    </a-tooltip>
    <FloMenu
      v-if="
        HomeContent.Default.MenuData.value.Id &&
        HomeContent.Default.ViewType.value == true
      "
    ></FloMenu>
    <div
      class="masking-out"
      v-if="HomeContent.Default.MenuData.value.Id"
      @click="maskingOut"
    ></div>
    <Multimeter v-if="HomeContent.Default.MulType.value"></Multimeter>

    <AngView v-show="HomeContent.Default.ViewType.value == true"></AngView>

    <!-- <module v-if="HomeContent.Default.ModuleShow.value"> </module> -->
    <div
      class="MultimeterStorage"
      @click="Storage"
      v-if="HomeContent.Default.ViewType.value == false"
    >
      放回
    </div>
    <Operations v-else></Operations>
    <Diagnos v-if="HomeContent.Default.DiagnosShow.value"></Diagnos>
    <Trobies v-if="HomeContent.Default.TrobiesShow.value"></Trobies>
    <Service v-if="HomeContent.Default.ServiceShow.value"></Service>
    <!-- <Evaluate></Evaluate> -->
    <TerminalInformation v-if="HomeContent.Default.TiType.value"></TerminalInformation>
    <div
      class="to_top"
      v-if="
        HomeContent.Default.tip.value == true &&
        HomeContent.Default.MouseLabel.value != ''
      "
    >
      {{ HomeContent.Default.MouseLabel.value }}
    </div>
    <div v-if="HomeContent.Default.ViewType.value == true">
      <div
        class="PerspectiveControl"
        @click="view_map"
        v-if="HomeContent.Default.WindowViewType.value"
      >
        <img src="/images/ic_map_select.png" class="ic_map" />
        <p>关闭视角</p>
      </div>
      <div class="PerspectiveControl" @click="view_map" v-else>
        <img src="/images/ic_map_normal.png" class="ic_map" />
        <p>显示视角</p>
      </div>
      <div class="flashlight" @click="controlFlashlight" v-if="Flashlight">
        <img src="/public/images/ic_flashlight_select@3x.png" class="ic_map" />
        <p>关闭手电筒</p>
      </div>
      <div class="flashlight" @click="controlFlashlight" v-else>
        <img src="/public/images/ic_flashlight_normal@3x.png" class="ic_map" />
        <p>打开手电筒</p>
      </div>
      <div class="roam" @click="controlRoam" v-if="roam">
        <img src="/public/images/ic_roaming_select@3x.png" class="ic_map" />
        <p>关闭漫游</p>
      </div>
      <div class="roam" @click="controlRoam" v-else>
        <img src="/public/images/ic_roaming_normal@3x.png" class="ic_map" />
        <p>开启漫游</p>
      </div>
    </div>
    <WearDevice></WearDevice>
    <root-board v-if="whiteboardTypes == true" />
  </div>
  <module
    v-if="HomeContent.Default.ModuleShow.value && HomeContent.Default.WOT.value == false"
  >
  </module>
  <moduleSub v-if="HomeContent.Default.ModuleShow.value && HomeContent.Default.WOT.value">
  </moduleSub>
  <Ultimate v-if="HomeContent.Default.EvaluateShow.value"></Ultimate>
  <messageBox
    ref="messageBoxRef"
    :Questions="'上传成功，点击确认退出软件'"
    :messageType="0"
  ></messageBox>
  <div class="BoxTip" v-if="HomeContent.Default.popTypes.value">
    <img src="/public/images/ic_Successful.png" class="ic_Successful" />
    <!-- <div class="TipTit">
            裁判
        </div> -->
    <div class="TipTxt">
      {{ HomeContent.Default.messageCodeResult }}
    </div>
    <div class="TipBtn" @click="TipCol">确定</div>
  </div>
</template>

<script setup lang="ts">
import hdr from "../Hdr/index.vue";
import { onMounted, ref, nextTick, reactive, onUnmounted } from "vue";
import { EditOutlined, RollbackOutlined, UploadOutlined } from "@ant-design/icons-vue";
import { useRouter } from "vue-router";
import FloMenu from "../FloatingMenu/index.vue";
import TerminalInformation from "../TerminalInformation/index.vue";
import Multimeter from "../Multimeter/index.vue";
import AngView from "../AngleofView/index.vue";
import module from "../WorkOrder/index.vue";
import moduleSub from "../WorkOrderSub/index.vue";
import Evaluate from "../Evaluate/index.vue";
import Ultimate from "../Ultimate/index.vue";
import Diagnos from "../DiagnosticInstrument/index.vue";
import Service from "../ServiceManual/index.vue";
import Trobies from "../Troubleshooting/index.vue";
import Operations from "../Operations/index.vue";
import RootBoard from "../elecboard/RootBoard.vue";
import messageBox from "../messageBox.vue";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import HomeContent from "../HomeContent";
import MessageCode from "../MessageCode";
import statComponen from "../StateCode";
import ObjectControllerCode from "../ObjectControllerCode";
import WearDevice from "../WearDevice/index.vue";
HomeContent.Default.isLoading.value = true;
let whiteboardTypes = ref(false);
let MouseLabelX = ref("0%");
let MouseLabelY = ref("0%");
let newXY: any = {};
let refNum = ref(0);
let ViewType = ref(false);
function requestCoordinate() {
  newXY = JSON.parse(window.sessionStorage.getItem("Cursor")!);
  MouseLabelX.value = newXY.X * window.screen.width * 0.01 + 10 + "px";
  MouseLabelY.value = newXY.Y * window.screen.height * 0.01 + 10 + "px";
  refNum.value = requestAnimationFrame(requestCoordinate);
}
const router = useRouter();
const StatusConfigs = ref(
  new Map([
    //车辆控制
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_START, "OFF"],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_PARKING, "Activated"],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_Gears, "P"],

    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LOCK_CAR_WINDOW,
      "Activated",
    ],

    //舱盖锁
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_HOOD_SWITCH, "Activated"],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_TRUNK_SWITCH, "Activated"],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LOCK_CAR_DOOR, "Activated"],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_UNLOCK_CAR_DOOR,
      "Activated",
    ],
    [ObjectControllerCode.Default.MsgCodesList.TRUNK_CLOSE_SWITCH, "Activated"],
    [ObjectControllerCode.Default.MsgCodesList.TRUNK_MACHINERY_SWITCH, "Activated"],

    //后视镜
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_REARVIEW_MIRROR_FOLD,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_REARVIEW_MIRROR_HEATER,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList
        .APPEARENCE_UI_REARVIEW_MIRROR_ACTIVATE_LEFT,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList
        .APPEARENCE_UI_REARVIEW_MIRROR_ACTIVATE_OFF,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList
        .APPEARENCE_UI_REARVIEW_MIRROR_ACTIVATE_RIGHT,
      "Activated",
    ],
    //灯光开关
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_TURN_LEFT,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_TURN_RIGHT,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_HEAD_NEAR,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_HEAD_DISTANCE,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_HEAD_FLASH,
      "Activated",
    ],

    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_AUTO, "Activated"],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_OFF, "Activated"],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_LAMPLET_SWITCH,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_HEADLAMP_SWITCH,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_FRONT_FOG,
      "Activated",
    ],
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_LIGHT_REAR_FOG, "Activated"],
    //雨刮开关
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_ONCE,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_CLSOE,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_SPRAY,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_INTERVAL,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_LOW,
      "Activated",
    ],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_WIPER_FRONT_HIGHT,
      "Activated",
    ],
    //遥控器
    [ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_REMOTEKEY_LOCK, "Activated"],
    [
      ObjectControllerCode.Default.MsgCodesList.APPEARENCE_UI_REMOTEKEY_UNLOCK,
      "Activated",
    ],
  ])
);
function MessageMethod(Arr: any) {
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisAdapterMessage(
      MessageCode.Default.MsgCodesList.DISASSEMBLY_OBJECT_VEHICLE_CONTROLLER,
      JSON.stringify(Arr)
    )
  );
}
//返回
async function roll() {
  HomeContent.Default.isHide.value = true;
  // 清除正计时的计时器
  clearInterval(HomeContent.Default.timekeepingControl.value);
  HomeContent.Default.isLoading.value = false;
  HomeContent.Default.time.value = "00:00:00";
  // 重置加速踏板
  let newArr1 = {
    Id: ObjectControllerCode.Default.MsgCodesList.ACCELERATOR_PEDAL,
    Content: 0,
  };
  MessageMethod(newArr1);
  // 重置制动踏板
  let newArr2 = {
    Id: ObjectControllerCode.Default.MsgCodesList.BRAKE_PEDAL,
    Content: 0,
  };
  MessageMethod(newArr2);
  // 重置方向盘
  let newArr3 = {
    Id: ObjectControllerCode.Default.MsgCodesList.STEERING_WHEEL,
    Content: 0,
  };
  MessageMethod(newArr3);
  // 重置万用表UI
  HomeContent.Default.MulType.value = false;
  // 重置车辆控制状态
  // Map数组循环取值
  for (let i of StatusConfigs.value.entries()) {
    statComponen.Default.StatusConfigs.value.set(i[0], i[1]);
  }

  //系统重置
  await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.SYSTEM_RESET
    )
  );

  HomeContent.Default.EvaluateShow.value = false;
  HomeContent.Default.SubBut.value = true;

  HomeContent.Default.currentPoint.value = "0";
  //重置
  await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.SYSTEM_QUIT
    )
  );
  //卸载
  await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.EmbeddedApplicationShutdownRequest()
  );

  router.push("/");
}
//放回
async function Storage() {
  WebSocketComponent.Default.PostMessage(new ClientMessage.DiagnosisDisplayRequest());
  HomeContent.Default.ViewType.value = true; // 为true不显示
}
function maskingOut() {
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisDisplayUIMenuRequest(false)
  );
  HomeContent.Default.MenuData.value = {};
}
const messageBoxRef = ref<any>();

async function sub() {
  HomeContent.Default.isHide.value = false;
  // 提交后关闭定时器，元素隐藏
  clearInterval(HomeContent.Default.timekeepingControl.value);
  HomeContent.Default.isLoading.value = false;
  // HomeContent.Default.time.value = "00:00:00";
  clearInterval(HomeContent.Default.timerControl.value);
  HomeContent.Default.timerControl.value = null;
  //获取评价数据
  let newArr: any = await WebSocketComponent.Default.PostMessageAsync(
    new ClientMessage.DiagnosisAdapterRequest(
      MessageCode.Default.MsgCodesList.APPRAISE_SUBMIT,
      `${Math.trunc((+new Date() - HomeContent.Default.StartTime.value) / 1000)}`
    )
  );
  console.log("提交", JSON.parse(newArr.Content).Result);
  HomeContent.Default.Synthetical.value = JSON.parse(
    newArr.Content
  ).Result.Record.Synthetical;

  HomeContent.Default.FaultRepair.value = JSON.parse(
    newArr.Content
  ).Result.Record.FaultRepair;
  HomeContent.Default.AED.value = JSON.parse(newArr.Content).Result;
  HomeContent.Default.WorksheetData.value =
    HomeContent.Default.AED.value.Record.RatingForm.RatingForm.RatingFormGroupModels;
  HomeContent.Default.PlayerReportData.value =
    HomeContent.Default.AED.value.Orders.Orders;

  console.log(
    "HomeContent.Default.AED.value==============",
    HomeContent.Default.AED.value
  );
  console.log("把时间，分数和个人信息传到提交接口里#####################");
  // 把时间，分数和个人信息传到提交接口里
  const localData: any = {
    score: HomeContent.Default.AED.value.Score,
    time: HomeContent.Default.AED.value.UsedTime,
    info: HomeContent.Default.UserName.value,
  };
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisAdapterMessage(
      MessageCode.Default.MsgCodesList.SYSTEM_LOCALSTORAGE_SAVE_CACHE,
      JSON.stringify(localData)
    )
  );
  // 全部信息提交接口里
  WebSocketComponent.Default.PostMessage(
    new ClientMessage.DiagnosisAdapterMessage(
      MessageCode.Default.MsgCodesList.SYSTEM_LOCALSTORAGE_SAVE,
      JSON.stringify(JSON.parse(newArr.Content).Result)
    )
  );

  if (HomeContent.Default.CacheFullData.value) {
    if (HomeContent.Default.CacheFullData.value.FaultPointCollection) {
      let license = JSON.parse(newArr.Content).Result;

      license.Orders.OrderBase.Time = HomeContent.Default.Duration.value;

      license.Id = HomeContent.Default.CacheFullData.value.ResultId;
      license.Orders = JSON.stringify(license.Orders);
      license.Record = JSON.stringify(license.Record);
      license.Tool = 0;
      license.Standard = 0;
      license.Process = 0;
      license.Security = 0;
      license.Efficiency = 0;
      license.Score = HomeContent.Default.AED.value.Score;
      license.Time = HomeContent.Default.AED.value.UsedTime;

      if (HomeContent.Default.CacheFullData.value.Mode == "4") {
        let response = await fetch(
          "http://virtual.eduhenghong.cn:4497/api/SkillsContest/SubmitResult",
          {
            method: "POST",
            body: JSON.stringify(license),
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${HomeContent.Default.CacheFullData.value.Token}`,
            },
          }
        );
      } else {
        let response = await fetch(
          "http://virtual.eduhenghong.cn:4497/api/TrainingResult/Submit",
          {
            method: "POST",
            body: JSON.stringify(license),
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${HomeContent.Default.CacheFullData.value.Token}`,
            },
          }
        );
      }
    }
  }
  if (HomeContent.Default.BDT.value == "0") {
    HomeContent.Default.EvaluateShow.value = true;
  } else {
    messageBoxRef.value.showModel();
  }
  HomeContent.Default.SubBut.value = false;
}
//电子白板
function whiteboard() {
  whiteboardTypes.value = !whiteboardTypes.value;
}
function TipCol() {
  HomeContent.Default.popTypes.value = false;
}
function view_map() {
  HomeContent.Default.WindowViewType.value = !HomeContent.Default.WindowViewType.value;
}
const Flashlight = ref(false);
async function controlFlashlight() {
  // 手电筒消息 + 切换
  Flashlight.value = !Flashlight.value;
  // await WebSocketComponent.Default.PostMessageAsync(
  //   new ClientMessage.FlashlightShowRequest(Flashlight.value)
  // );
}
const roam = ref(false);
async function controlRoam() {
  // 漫游消息 + 切换
  roam.value = !roam.value;
  // await WebSocketComponent.Default.PostMessageAsync(
  //   new ClientMessage.RoamModeRequest(roam.value)
  // );
}
onMounted(() => {
  refNum.value = requestAnimationFrame(requestCoordinate);
  //存贮程序开始运行时间
  HomeContent.Default.StartTime.value = new Date();
  console.log(HomeContent.Default.StartTime.value);
});
</script>

<style scoped>
.wp {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  position: relative;
  color: #fff;
  background: transparent;
  overflow: hidden;
}

.MultimeterStorage {
  width: 200px;
  position: absolute;
  left: 50%;
  bottom: 10%;
  height: 60px;
  border-radius: 8px;
  transform: translate(-50%, -50%);
  background-color: #fff;
  line-height: 60px;
  text-align: center;
  font-size: 24px;
  color: #000;
}

.gongdan {
  font-size: 50px;
  color: #000;
}

.masking-out {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.01);
}

.submitTo {
  color: rgb(255, 255, 255) !important;
  font-size: 24px;
  position: absolute;
  top: 1.5vh;
  right: 170px;
}

.submitToRoll {
  color: rgb(255, 255, 255) !important;
  font-size: 24px;
  position: absolute;
  top: 1.5vh;
  right: 120px;
}

.edit {
  color: rgb(255, 255, 255) !important;
  font-size: 24px;
  position: absolute;
  top: 1.5vh;
  right: 70px;
}

.ico_roll {
  font-size: 24px;
  position: absolute;
  right: 120px;
  top: 1.5vh;
}

.to_top {
  font-size: 20px;
  text-align: center;
  position: absolute;
  color: #fff;
  left: v-bind(MouseLabelX);
  top: v-bind(MouseLabelY);
  padding: 5px 10px;
  background: rgba(13, 20, 59, 0.36);
  border: 1px solid rgba(53, 196, 255, 0.34);
}

.PerspectiveControl {
  color: #fff;
  position: absolute;
  bottom: 10px;
  text-align: center;
  right: 20px;
  background-color: rgba(0, 0, 0, 0.01);
}
.flashlight {
  color: #fff;
  position: absolute;
  bottom: 10px;
  text-align: center;
  right: 103px;
  background-color: rgba(0, 0, 0, 0.01);
}

.roam {
  color: #fff;
  position: absolute;
  bottom: 10px;
  text-align: center;
  right: 200px;
  background-color: rgba(0, 0, 0, 0.01);
}
.ic_map {
  width: 40px;
  margin-bottom: 10px;
}

.ViewTypes {
  display: none !important;
}

/* .BoxTip {
  width: 400px;
  height: 190px;
  border-radius: 15px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
.TipTxt {
  font-size: 18px;
  margin: 65px 0 0 0;
  color: #000;
  text-align: center;
}

.TipBtn {
  width: 76px;
  height: 36px;
  color: black;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  bottom: 20px;
  text-align: center;
  line-height: 36px;
  font-size: 16px;
  transform: translate(-50%, 0);
  border: 1px solid black;
}

.TipBtn:hover {
  background-color: #9bfb4e;
} */
.BoxTip {
  width: 360px;
  height: 294px;
  border-radius: 16px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.ic_Successful {
  width: 128px;
  margin-top: 20px;
  margin-left: 116px;
}

.TipTxt {
  font-size: 16px;
  font-weight: 400;
  color: #202020;
  position: absolute;
  left: 51%;
  transform: translate(-50%);
  top: 57%;
}

.TipBtn {
  width: 112px;
  height: 48px;
  color: black;
  border-radius: 6px;
  position: absolute;
  left: 50%;
  bottom: 20px;
  text-align: center;
  line-height: 48px;
  font-size: 16px;
  transform: translate(-50%, 0);
  border: 1px solid #e9e9e9;
}

.TipBtn:hover {
  background-color: #9bfb4e;
}
</style>
